/*
 * Created on 2006-9-15
 *
 * Organization: BearingPoint Inc.
 */
package com.eaoa.uc.account.bo;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.eaoa.base.EABaseBusinessObject;
import com.eaoa.dao.pub.PubAccountDAO;
import com.eaoa.query.pub.PubAccountQuery;
import com.eaoa.query.pub.PubCustomerQuery;
import com.eaoa.vo.pub.PubAccount;
import com.eaoa.vo.pub.PubCustomer;


public class AccountGetPermissionidBO extends EABaseBusinessObject {

	private PubAccount employee;
	
    private static PubAccountDAO accountDAO = new PubAccountDAO();
    
	public AccountGetPermissionidBO(PubAccount employee) {
		super();
		this.employee = employee;
	}

	protected void performBusinessLogic() {
		
		PubAccount employeeTemp = PubAccountQuery.loadByNoInitPubCustomers(employee.getAccountno());
		
		Set<Long> permissionids = getPermissionids(employeeTemp);
		Set<Long> customerids = getCustomerids(employeeTemp);
		Set<Long> customerAccountids = getCustomerAccountids(employeeTemp, customerids);
		Set<Long> masterAccountids = getMasterAccountids(employeeTemp);
		
		employee.setPermissionids(permissionids);
		employee.setCustomerids(customerids);
		employee.setCustomerAccountids(customerAccountids);
		employee.setMasterAccountids(masterAccountids);
	}

	public Set<Long> getPermissionids(PubAccount employeeTemp) {
		Set<Long> permissionids = new HashSet<Long>();
		permissionids =  accountDAO.getAccountPermissionIds(employeeTemp.getAccountno());
		if(permissionids.size()==0)
			permissionids.add(new Long(0));
		return permissionids;
	}

	public Set<Long> getCustomerids(PubAccount employeeTemp) {
		Set<Long> customerids = new HashSet<Long>();
		Iterator customers  = employeeTemp.getPubCustomers().iterator();
		while(customers.hasNext()){
			PubCustomer customer = (PubCustomer)customers.next();
			customerids.add(customer.getCustomerno());
		}
		if(customerids.size()==0)
			customerids.add(new Long(0));
		return customerids;
	}

	public Set<Long> getCustomerAccountids(PubAccount employeeTemp, Set<Long> customerids) {
		Set<Long> customerAccountids = new HashSet<Long>();
		PubAccountQuery  accountQuery  = new PubAccountQuery();
		accountQuery.setCompanyno(customerids);
		accountQuery.setIslock("N");
		accountQuery.execute();
		Iterator customerAccounts  = accountQuery.getResults().iterator();
		while(customerAccounts.hasNext()){
			PubAccount customerAccount = (PubAccount)customerAccounts.next();
			customerAccountids.add(customerAccount.getAccountno());
		}
		if(customerAccountids.size()==0)
			customerAccountids.add(new Long(0));
		return customerAccountids;
	}

	public Set<Long> getMasterAccountids(PubAccount employeeTemp) {
		Set<Long> masterAccountids = new HashSet<Long>();
		PubCustomer customer = PubCustomerQuery.loadByIdFetchPubAccounts(employeeTemp.getCompany().getCustomerno());
		Iterator masters = customer.getPubAccounts().iterator();
		while(masters.hasNext()){
			PubAccount master = (PubAccount)masters.next();
			masterAccountids.add(master.getAccountno());
		}
		if(masterAccountids.size()==0)
			masterAccountids.add(new Long(0));
		return masterAccountids;
	}


}